package com.zyw.customcalendar.dao;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;

import com.zyw.customcalendar.AppApplication;
import com.zyw.customcalendar.model.EventModel;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class CalendarSQLiteDao {

	private CalendarSQLite sqlite;

	public CalendarSQLiteDao(){
		this.sqlite = AppApplication.sqlite;
	}

	public CalendarSQLiteDao(Context context){
		this.sqlite = new CalendarSQLite(context, "calendar_db3", null, 1);
	}

	public EventModel getData(String startTime){
		EventModel model = new EventModel();
		SQLiteDatabase db = sqlite.getReadableDatabase();
		Cursor cursor = db.query(sqlite.CALENDAR_EVENT_TABLE_NAME, 
				new String[]{"*"}, " start_time = ? ", new String[]{startTime}, null, null, null);
		cursor.moveToNext();
		model = CommonRead(model, cursor);
		return model;
	}

	public void getEventsLikeMonth(String monthString,HashMap<String,Integer> eventDays){
		monthString = monthString + "%";
		SQLiteDatabase db = sqlite.getReadableDatabase();
		Cursor cursor = db.query(sqlite.CALENDAR_EVENT_TABLE_NAME,
				new String[]{"start_date", "count(*)"}, " start_date like ? ", new String[]{monthString}, "start_date", null, null);

		if(cursor != null && cursor.getCount() > 0){
			while (cursor.moveToNext()) {
				String startTimeString = cursor.getString(0);
				int num = Integer.parseInt(cursor.getString(1));
				android.util.Log.i("zyw","startTimeString="+startTimeString);
				eventDays.put(startTimeString, num);
			}
		}
		cursor.close();
	}

	public ArrayList<EventModel> getLikeData(String startTime){
		startTime = startTime + "%";
		SQLiteDatabase db = sqlite.getReadableDatabase();
		Cursor cursor = db.query(sqlite.CALENDAR_EVENT_TABLE_NAME,
				new String[]{"*"}, " start_time like ? ", new String[]{startTime}, null, null, null);
		ArrayList<EventModel> modelist = new ArrayList<>();
		if(cursor != null && cursor.getCount() > 0){
			while(cursor.moveToNext()){
				EventModel model = new EventModel();
				modelist.add(CommonRead(model, cursor));
			}
		}else return null;
		return modelist;
	}

	public ArrayList<EventModel> getAllData(){
		EventModel model = new EventModel();
		SQLiteDatabase db = sqlite.getReadableDatabase();
		Cursor cursor = db.query(sqlite.CALENDAR_EVENT_TABLE_NAME, 
				new String[]{"*"}, "", new String[]{}, null, null, null);

		ArrayList<EventModel> modelist = new ArrayList<>();
		if(cursor != null && cursor.getCount() > 0){
			while(cursor.moveToNext()){
				model = CommonRead(model, cursor);
				modelist.add(model);
			}
		}else return null;
		return modelist;
	}

	public EventModel CommonRead(EventModel model, Cursor cursor){
		Field[] prFields=model.getClass().getDeclaredFields();
		for(Field field:prFields)
			field.setAccessible(true);
		for(int i = 0; i < model.columns.length; i++){
			try {
				String columnString = cursor.getString(cursor.getColumnIndex(model.columns[i]));
				Field property=model.getClass().getField(model.fields[i]);
				property.set(model, columnString);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return model;
	}

	public int setData(EventModel model){
		SQLiteDatabase db = sqlite.getWritableDatabase();
		ContentValues values = new ContentValues();
		for (int i = 1; i < model.columns.length; i++) {
			try {
				Field property = model.getClass().getField(model.fields[i]);
				String value = (String) property.get(model);
				if(value != null) {
					values.put(model.columns[i], value);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(model.id == null) {
			return (int) db.insert(sqlite.CALENDAR_EVENT_TABLE_NAME, null, values);
		}else {
			return db.update(sqlite.CALENDAR_EVENT_TABLE_NAME, values, " where _id = ? ",new String[]{model.id});
		}
	}

}
